package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.utils.Constants;
import com.mzxy.boxuegu.view.MyInfoView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tvBack;
    private TextView tvMainTitle;
    private RelativeLayout titleBar;
    private FrameLayout flMainBody;
    private RelativeLayout rlBottomBarCourseBtn;
    private RelativeLayout rlBottomBarExercisesBtn;
    private RelativeLayout rlBottomBarMyinfoBtn;
    private TextView tvBottomBarTextCourse;
    private TextView tvBottomBarTextExercises;
    private TextView tvBottomBarTextMyinfo;
    private ImageView ivBottomBarImageCourse;
    private ImageView ivBottomBarImageExercises;
    private ImageView ivBottomBarImageMyinfo;

    private MyInfoView myInfoView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        //设置标题栏
        findViewById(R.id.title_bar).setBackgroundColor(Color.parseColor("#30B4FF"));
        tvBack.setVisibility(View.GONE);
        selectDisplayView(0);
    }

    private void initEvent() {
        rlBottomBarCourseBtn.setOnClickListener(this);
        rlBottomBarExercisesBtn.setOnClickListener(this);
        rlBottomBarMyinfoBtn.setOnClickListener(this);
    }

    private void initView() {
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        flMainBody = (FrameLayout) findViewById(R.id.main_body);
        titleBar = (RelativeLayout) findViewById(R.id.title_bar);
        rlBottomBarCourseBtn = (RelativeLayout) findViewById(R.id.bottom_bar_course_btn);
        rlBottomBarExercisesBtn = (RelativeLayout) findViewById(R.id.bottom_bar_exercises_btn);
        rlBottomBarMyinfoBtn = (RelativeLayout) findViewById(R.id.bottom_bar_myinfo_btn);
        tvBottomBarTextCourse = (TextView) findViewById(R.id.bottom_bar_text_course);
        tvBottomBarTextExercises = (TextView) findViewById(R.id.bottom_bar_text_exercises);
        tvBottomBarTextMyinfo = (TextView) findViewById(R.id.bottom_bar_text_myinfo);
        ivBottomBarImageCourse = (ImageView) findViewById(R.id.bottom_bar_image_course);
        ivBottomBarImageExercises = (ImageView) findViewById(R.id.bottom_bar_image_exercises);
        ivBottomBarImageMyinfo = (ImageView) findViewById(R.id.bottom_bar_image_myinfo);
    }


    public void toRegister(View view){
        startActivity(new Intent(this,RegisterActivity.class));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bottom_bar_course_btn :
                selectDisplayView(0);
                break;
            case R.id.bottom_bar_exercises_btn :
                selectDisplayView(1);
                break;
            case R.id.bottom_bar_myinfo_btn :
                selectDisplayView(2);
                break;
        }
    }

    private void selectDisplayView(int index ){
        titleBar.setVisibility(View.VISIBLE);
        removeAllView();
        createView(index);
        setSelectedStatus(index);
    }

    private void removeAllView() {

        for(int i = 0 ; i < flMainBody.getChildCount() ; i++){
            flMainBody.getChildAt(i).setVisibility(View.GONE);
        }

    }

    private View courceView;
    private View exercisesView;



    private void createView(int index){
        switch (index){
            case 0:
                if(courceView == null){
                    courceView = LayoutInflater.from(this).inflate(R.layout.testv1,null);
                    flMainBody.addView(courceView);
                }
                courceView.setVisibility(View.VISIBLE);
                break;
            case 1:
                if(exercisesView == null){
                    exercisesView = LayoutInflater.from(this).inflate(R.layout.testv2,null);
                    flMainBody.addView(exercisesView);
                }
                exercisesView.setVisibility(View.VISIBLE);
                break;
            case 2:
                if(myInfoView == null){
                    myInfoView = new MyInfoView(this);
                    flMainBody.addView(myInfoView.getView());
                }
                myInfoView.showView();
                break;
        }
    }


    /**
     * 清空底部按钮的选中状态
     */
    private void clearBottomImageState(){
        tvBottomBarTextCourse.setTextColor(Color.parseColor("#666666"));
        tvBottomBarTextExercises.setTextColor(Color.parseColor("#666666"));
        tvBottomBarTextMyinfo.setTextColor(Color.parseColor("#666666"));
        ivBottomBarImageCourse.setImageResource(R.drawable.main_course_icon);
        ivBottomBarImageExercises.setImageResource(R.drawable.main_exercises_icon);
        ivBottomBarImageMyinfo.setImageResource(R.drawable.main_my_icon);
    }

    private void setSelectedStatus(int index){
        clearBottomImageState();
        switch (index){
            case 0:  //课程
                tvBottomBarTextCourse.setTextColor(Color.parseColor("#0097F7"));
                ivBottomBarImageCourse.setImageResource(R.drawable.main_course_icon_selected);
                tvMainTitle.setText("博学谷课程");
                break;
            case 1:  //习题
                tvBottomBarTextExercises.setTextColor(Color.parseColor("#0097F7"));
                ivBottomBarImageExercises.setImageResource(R.drawable.main_exercises_icon_selected);
                tvMainTitle.setText("博学谷习题");
                break;
            case 2:  //我
                tvBottomBarTextMyinfo.setTextColor(Color.parseColor("#0097F7"));
                ivBottomBarImageMyinfo.setImageResource(R.drawable.main_my_icon_selected);
                tvMainTitle.setText("我");
                break;
        }
    }

    protected long exitTime;  //记录第一次点击的时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //拦截返回按钮，并且是抬起按键的那一瞬间
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            //如果两次点击的时间间隔小于2秒，则认为你是要退出app
            if(System.currentTimeMillis() - exitTime < 1000 ){
                finish();
                System.exit(0);
            }else{
                exitTime = System.currentTimeMillis();
                Toast.makeText(this,"再按一次退出博学谷",Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if((requestCode == Constants.MYINFO2LOGIN_REQUEST_CODE) &&
                (resultCode == Constants.LOGIN2MYINFO_RESULT_CODE)){
            //这次登陆是从 “我”的界面发起的，并且数据数回传给“我”
            //在这里 获取回传的 用户名，并且设置到MyInfoView里面去
            String username = data.getStringExtra("username");
            myInfoView.setLoginParam(username);
        }else if((requestCode == Constants.MYINFO2SETTING_REQUEST_CODE) &&
                (resultCode == Constants.SETTING2MYINFO_RESULT_CODE)){
            //代表从设置界面跳转过来的页面
            boolean exitLogin = data.getBooleanExtra("exitLogin",false);
            if(exitLogin){
                myInfoView.setLoginParam(null);
            }
        }





    }
}
